let express = require('express');
var fs = require("fs");
let router = express.Router();
// let _ = require("lodash");

//读文件
let cont;
fs.readFile("data/oneDream.json", (a, b) => {
    if (a) {
        console.log(a);
    } else {
        cont = JSON.parse(b.toString());
    }
})

// 注册账号
router.post('/signUp', function(req, res, next) {
    let resData = {};
    let copyUserDB = [...cont.userDB];
    let newData = copyUserDB.filter(el => {
        if (el.user === req.body.user) {
            return el;
        }
    })
    if (newData.length > 0) {
        resData.isOk = false;
    } else {
        cont.userDB.push(req.body);
        fs.writeFile("data/oneDream.json", JSON.stringify(cont), function(err) {
            if (err) {
                return console.log(err);
            }
        });
        resData.isOk = true;
    }
    res.json(resData);
});



module.exports = router;